﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.ComponentModel.Composition;
using System.Web.Http;
using Mojito.Service;
using Mojito.Entity;
using System.Diagnostics;
using Mojito.WebSite.Models;
using Sohobloo.SoCommon;
using Sohobloo.SoMcCoffee.Entity;

namespace Mojito.WebSite.Controllers
{
    [Export]
    public class DishCategoryAPIController : ApiControllerBase
    {
        [Import]
        public IDishCategoryService DishCategoryService { get; set; }

        [HttpGet]
        public IEnumerable<DishCategoryVM> Get()
        {
            var data = DishCategoryService.GetDishCategoryList();
            var t = data as IEnumerable<EntityBase>;
            return data.Select(dishCategory => new DishCategoryVM(dishCategory)).ToList();
        }

        [HttpGet]
        public HttpResponseMessage Get(string id)
        {
            HttpResponseMessage response;
            Guid guid;
            if (!Guid.TryParse(id, out guid))
            {
                LogError("Parse guid error:[" + id + "]");
                response = Request.CreateResponse(HttpStatusCode.BadRequest, id);
            }
            else
            {
                var data = DishCategoryService.GetDishCategory(guid);
                var t = data as IEnumerable<EntityBase>;
                response = Request.CreateResponse(HttpStatusCode.OK, new DishCategoryVM(data));
            }
            return response;
        }

        [HttpPost]
        public HttpResponseMessage Add(string id)
        {
            HttpResponseMessage response;
            var result = DishCategoryService.AddDishCategory(id);
            if (result.ResultType == OperationResultType.Success)
            {
                response = Request.CreateResponse(HttpStatusCode.Created, new DishCategoryVM(result.AppendData as DishCategory));
            }
            else if (result.ResultType == OperationResultType.Exist)
            {
                response = Request.CreateResponse(HttpStatusCode.NotModified, id);
            }
            else
            {
                response = Request.CreateResponse(HttpStatusCode.InternalServerError, id);
            }
            return response;
        }

        [HttpPut]
        public HttpResponseMessage Update(string id)
        {
            var s = Request.Content.ReadAsStringAsync().Result;
            HttpResponseMessage response;
            Guid guid;
            if (!Guid.TryParse(id, out guid))
            {
                LogError("Parse guid error:[" + id + "]");
                response = Request.CreateResponse(HttpStatusCode.BadRequest, id);
            }
            else
            {
                var result = DishCategoryService.UpdateDishCategory(guid, s);
                if (result.ResultType == OperationResultType.Success)
                {
                    response = Request.CreateResponse(HttpStatusCode.OK, new DishCategoryVM(result.AppendData as DishCategory));
                }
                else if (result.ResultType == OperationResultType.NotExist)
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound, id);
                }
                else
                {
                    response = Request.CreateResponse(HttpStatusCode.InternalServerError, id);
                }
            }
            return response;
        }

        [HttpDelete]
        public HttpResponseMessage Delete(string id)
        {
            HttpResponseMessage response;
            Guid guid;
            if (!Guid.TryParse(id, out guid))
            {
                LogError("Parse guid error:[" + id + "]");
                response = Request.CreateResponse(HttpStatusCode.BadRequest, id);
            }
            else
            {
                var result = DishCategoryService.DeleteDishCategory(guid);
                if (result.ResultType == OperationResultType.NotExist)
                {
                    response = Request.CreateResponse(HttpStatusCode.NotFound, "Item not found");
                }
                else if (result.ResultType == OperationResultType.Success)
                {
                    response = Request.CreateResponse(HttpStatusCode.NoContent);
                }
                else
                {
                    response = Request.CreateResponse(HttpStatusCode.InternalServerError, id);
                }
            }
            return response;
        }
    }
}
